#region Using Directives

using System;
using Eneta.Portal.Forum.Common.Data;
using Eneta.Portal.Forum.Core.Domain;
using Eneta.Portal.Forum.Core.Domain.Entities;
using Eneta.Portal.Forum.Core.Service.Application;
using Microsoft.SharePoint;

#endregion

namespace Eneta.Portal.Forum.Core.Data
{
    public class TopicMapper
    {
        /// <summary>
        /// Creates the dto.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <returns></returns>
        public static SharePointListItem CreateDto(Topic topic)
        {
            string[] topicValues = {
                                       "Title", topic.Name,
                                       "ForumID", topic.ForumId.ToString(),
                                       "Views", topic.Views.ToString(),
                                       "TopicStarterID", topic.TopicStarterId.ToString(),
                                       "NumPosts", topic.NumPosts.ToString()
                                   };

            return new SharePointListItem(topic.Id, topicValues);
        }

        /// <summary>
        /// Creates the domain object.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static Topic CreateDomainObject(SharePointListItem item)
        {
            Topic topic = new Topic(item.Id, Convert.ToInt32(item["ForumID"]), item["Title"]);

            topic.Views = Convert.ToInt32(item["Views"]);
            topic.TopicStarterId = Convert.ToInt32(item["TopicStarterID"]);
            topic.Author = RepositoryRegistry.ForumUserRepository.GetBySharePointId(topic.TopicStarterId);
			
            if(string.IsNullOrEmpty(item["NumPosts"]))
                topic.NumPosts = 0;
            else
                topic.NumPosts = Convert.ToInt32(item["NumPosts"]);

            var messages = topic.Messages;
            messages.Sort("Modified", SortDirection.Descending);
            if(messages.Count > 0)
                topic.LastPost = Convert.ToDateTime(messages[messages.Count-1].Modified);

            return topic;
        }
    }
}